'use strict';

const Controller = require('egg').Controller;

class AppointmentsController extends Controller {
  // 创建预约
  async create() {
    const { ctx } = this;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    const { property_id, appointment_date, contact_phone, contact_name, remark } = ctx.request.body;

    if (!property_id || !appointment_date || !contact_phone) {
      ctx.status = 400;
      ctx.body = { success: false, message: '必填字段不能为空' };
      return;
    }

    try {
      const result = await ctx.service.appointment.create({
        property_id,
        user_id,
        appointment_date,
        contact_phone,
        contact_name,
        remark
      });

      ctx.status = 201;
      ctx.body = { 
        success: true, 
        message: '预约提交成功', 
        data: { id: result.insertId } 
      };
    } catch (e) {
      ctx.status = 400;
      ctx.body = { success: false, message: e.message };
    }
  }

  // 获取用户的预约列表
  async getUserAppointments() {
    const { ctx } = this;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    try {
      const data = await ctx.service.appointment.getUserAppointments(user_id, ctx.query);
      ctx.body = { success: true, data };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: '获取预约列表失败' };
    }
  }

  // 获取房源的预约列表（房东查看）
  async getPropertyAppointments() {
    const { ctx } = this;
    const { propertyId } = ctx.params;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    // 检查是否是房源所有者
    const property = await ctx.service.property.detail(propertyId);
    if (!property || property.user_id !== user_id) {
      ctx.status = 403;
      ctx.body = { success: false, message: '无权限查看' };
      return;
    }

    try {
      const data = await ctx.service.appointment.getPropertyAppointments(propertyId, ctx.query);
      ctx.body = { success: true, data };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: '获取预约列表失败' };
    }
  }

  // 更新预约状态
  async updateStatus() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { status } = ctx.request.body;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    if (!['pending', 'confirmed', 'cancelled', 'completed'].includes(status)) {
      ctx.status = 400;
      ctx.body = { success: false, message: '状态值无效' };
      return;
    }

    try {
      await ctx.service.appointment.updateStatus(id, status, user_id);
      ctx.body = { success: true, message: '状态更新成功' };
    } catch (e) {
      ctx.status = 400;
      ctx.body = { success: false, message: e.message };
    }
  }

  // 取消预约
  async cancel() {
    const { ctx } = this;
    const { id } = ctx.params;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    try {
      await ctx.service.appointment.cancel(id, user_id);
      ctx.body = { success: true, message: '预约已取消' };
    } catch (e) {
      ctx.status = 400;
      ctx.body = { success: false, message: e.message };
    }
  }

  // 获取预约详情
  async detail() {
    const { ctx } = this;
    const { id } = ctx.params;
    const user_id = ctx.state.user?.id;

    if (!user_id) {
      ctx.status = 401;
      ctx.body = { success: false, message: '请先登录' };
      return;
    }

    try {
      const data = await ctx.service.appointment.getDetail(id);
      if (!data) {
        ctx.status = 404;
        ctx.body = { success: false, message: '预约不存在' };
        return;
      }

      // 检查权限：预约者本人或房源所有者
      const property = await ctx.service.property.detail(data.property_id);
      if (data.user_id !== user_id && property.user_id !== user_id) {
        ctx.status = 403;
        ctx.body = { success: false, message: '无权限查看' };
        return;
      }

      ctx.body = { success: true, data };
    } catch (e) {
      ctx.status = 500;
      ctx.body = { success: false, message: '获取预约详情失败' };
    }
  }
}

module.exports = AppointmentsController;
